<template>
  <el-form-item :label="type" :prop="prop">
    <el-popover
      placement="bottom"
      :width="400"
      trigger="click"
      v-model:visible="cityPanelIsShow"
    >
      <template #reference>
        <el-input
          type="text"
          autocomplete="off"
          disableautocomplete
          v-model="currCityName"
          :placeholder="placeholder"
          readonly
        />
      </template>
      <span class="vcp-exit">
        <button @click="closeImm">×</button>
      </span>
      <div class="vue-city-selector">
        <!--城市选择-->

        <div
          class="vcp-panel"
          @mouseleave="MouseLeave"
          @mouseenter="MouseEnter"
        >
          <h5>
            城市选择
            <span class="clear-citypanel" @click="clearCityPanel">清空</span>
          </h5>
          <ul class="vcp-tags">
            <li
              :class="{ 'li-is-active': item == tagKey }"
              v-for="(item, index) in tagsArr"
              :key="index"
              @click="choiceTag(item)"
            >
              {{ item }}
            </li>
          </ul>

          <div class="vcp-list">
            <div
              :class="{ 'is-hot': initial == 'hot' }"
              v-for="(citys, initial) in cityList"
              :key="initial"
              v-show="currTagIncludes(initial)"
            >
              <h6 v-if="initial != 'hot'">{{ initial }}</h6>
              <ul>
                <li
                  v-for="v in citys"
                  @click="choiceCity(v.name)"
                  :title="v.name"
                  :key="v.id"
                >
                  {{ v.name }}
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </el-popover>
  </el-form-item>
</template>

<script>
import { defineComponent, ref, computed } from 'vue';
export default defineComponent({
  name: 'CityPicker',
  props: {
    type: String,
    prop: String,
    currCityName: {
      type: String,
      default: '',
      required: true,
    },
    placeholder: {
      type: String,
      default: '',
    },
    // 城市列表
    cityList: {
      type: Object,
    },
    // 没有热门城市
    noHot: {
      type: Boolean,
      default: false,
    },
  },
  setup(props, context) {
    //tags
    const tagsArr = ref(['热门', 'ABCDE', 'FGHJ', 'KLMN', 'PQRST', 'WXYZ']);
    const tagKey = ref('热门');
    const tags = ref({});
    if (props.noHot) {
      tagsArr.value = ['ABCD', 'EFGH', 'JKL', 'MNPQ', 'RSTW', 'XYZ'];
      tagKey.value = 'ABCD';
    }
    tagsArr.value.forEach((item) => {
      if (item === '热门') {
        tags.value[item]  = ['hot'];
      } else {
        tags.value[item] = item.split('');
      }
    });
    const currTagIncludes = (ini) => {
      // 列表是否属于当前标签
      return currTag.value.includes(ini);
    };
    const choiceTag = (key) => {
      // 选择标签
      tagKey.value = key;
    };
    //show
    const cityPanelIsShow = ref(false);
    const currTag = computed(function() {
      return tags.value[tagKey.value];
    });
    const clearCityPanel = () => {
      //清空
      context.emit('update:currCityName', '');
    };
    const closeImm = () => {
      // 立即关闭城市面板
      cityPanelIsShow.value = false;
    };
    const endChoice = () => {
      if (!isMouseOver.value) {
        closeImm();
      }
    };
    const choiceCity = (name) => {
      closeImm();
      context.emit('update:currCityName', name);
    };
    //mouse
    const isMouseOver = ref(false);
    const MouseEnter = () => {
      isMouseOver.value = true;
    };
    const MouseLeave = () => {
      isMouseOver.value = false;
    };
    return {
      cityPanelIsShow,
      tagsArr,
      tagKey,
      tags,
      isMouseOver,
      currTag,
      clearCityPanel,
      currTagIncludes,
      choiceTag,
      choiceCity,
      MouseEnter,
      MouseLeave,
      closeImm,
      endChoice,
    };
  },
});
</script>

<style lang="scss" scoped>
@import '@/assets/css/config.scss';
$defaultColor: #d0000e;
.vcp-exit {
  height: 10px;
  position: relative;
  top: -10px;
  right: -10px;
  margin: 0;
  padding: 0;
  button {
    background-color: transparent;
    border: none;
    float: right;
    cursor: pointer;
    font-size: 20px;
  }
}
.vue-city-selector {
  width: 200px;
  position: relative;
  margin: 0;
  padding: 0;
  font: 12px/1.5 'Microsoft YaHei', tahoma, arial, 'Hiragino Sans GB',
    '\\5b8b\4f53', sans-serif;
  color: #555;

  li {
    list-style-type: none;
  }

  input {
    display: block;
    width: 100%;
    height: 32px;
    line-height: 32px;
    padding-left: 6px;
    outline: none;
    font-size: 14px;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
  }

  .vcp-panel {
    position: relative;
    // z-index: 999999;
    left: 10px;
    padding: 12px 10px 0;
    width: 382px;
    min-height: 150px;
    overflow: hidden;
    background-color: #fff;
    animation: vueCitySelector 0.2s ease-out;
    box-sizing: border-box;

    h5 {
      font-size: 12px;
      padding: 0;
      margin: 0;
      margin-bottom: 5px;
      padding-left: 2px;
      color: #aaa;
      line-height: 1.5;
      font-weight: normal;
      position: relative;

      .clear-citypanel {
        position: absolute;
        width: 30px;
        right: 20px;
        top: 0;
        height: 18px;
        line-height: 18px;
        color: $defaultColor;
        cursor: pointer;
      }
    }

    .vcp-tags {
      overflow: hidden;
      margin: 5px 0;
      padding: 0;

      li {
        box-sizing: border-box;
        width: 60px;
        line-height: 20px;
        display: block;
        float: left;
        background-color: #f2f2f3;
        text-align: center;
        border-left: 1px solid #fff;
        cursor: pointer;

        &:first-child {
          border-left: none;
        }

        &:hover {
          color: $defaultColor;
        }

        &.li-is-active {
          background-color: $defaultColor;
          color: #fff;
        }
      }
    }

    .vcp-list {
      overflow: hidden;
      border-bottom: 10px solid #fff;

      & > div {
        margin: 1px 0 -1px;
        padding: 5px 0;
        line-height: 24px;
        border-bottom: 1px dashed #eee;
        overflow: hidden;
      }

      h6 {
        float: left;
        font-weight: normal;
        color: $defaultColor;
        font-size: 13px;
        width: 30px;
        margin: 0;
        padding: 0;
        padding-left: 5px;
      }

      ul {
        overflow: hidden;
        padding: 0;
        margin: 0;

        li {
          float: left;
          width: 60px;
          padding-left: 4px;
          cursor: pointer;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;

          &:hover {
            background-color: #f2f2f3;
            color: $defaultColor;
          }

          &.is-hot {
            padding-left: 10px;

            ul {
              padding: 0;

              li {
                padding-left: 6px;
              }
            }
          }
        }
      }
    }
  }
}

@keyframes vueCitySelector {
  0% {
    opacity: 0;
    transform: translate(0) scale(0);
  }

  100% {
    opacity: 1;
    transform: translate(0) scale(1);
  }
}
</style>
